package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.common.BaseServiceImpl;
import com.usian.common.constants.user.AdminConstants;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ArticleFeign;
import com.usian.user.feign.WemediaFeign;

import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.ApUserRealnameMapper;
import com.usian.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service

public class ApUserRealnameServiceImpl extends BaseServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {
    @Autowired
    private ArticleFeign articleFeign;
    @Autowired
    private WemediaFeign wemediaFeign;
    @Autowired
    private ApUserMapper apUserMapper;

    @Override
    public ResponseResult loadListByStatus(AuthDto dto) {
        if (dto == null) return errorResult;
        if (dto.getStatus() == null) {
            dto.setStatus((short) 1);
        }
        LambdaQueryWrapper<ApUserRealname> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApUserRealname::getStatus, dto.getStatus());
        Page page1 = new Page(dto.page, dto.size);
        IPage page = page(page1, wrapper);
        ResponseResult responseResult = new PageResponseResult(dto.page, dto.size, (int) page.getTotal());
        responseResult.setCode(0);
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    @Override
    /*@Transactional*/
   @GlobalTransactional
    public ResponseResult updateStatusById(AuthDto dto, Short status) {
        /*
         * 对ApUserRealname表进行查询修改
         * */
        //       判断参数  判断传过来的参数是否为空
        if (dto == null || dto.getId() == null || status == null || (status != AdminConstants.PASS_AUTH && status != AdminConstants.FAIL_AUTH))
            return errorResult;


        //  创建ApUserRealname对象将传过来的参数保存到里面，以供后面修改使用
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(dto.getId());
        apUserRealname.setStatus(status);
        if (dto.getMsg() != null) {
            apUserRealname.setReason(dto.getMsg());
        }
//       查询具体的ApUserRealname；并进行修改
        updateById(apUserRealname);

        /*
         *  认证通过添加自媒体账号和作者账号
         * */
//        如果传归来的状态是通过
        if (status.equals(AdminConstants.PASS_AUTH)) {
//            当状态为审核通过时查询apuser，
            ResponseResult createResult = createWmUserAndAuthor(dto);
            if (createResult != null) {
                return createResult;
            }
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /*
     * 如果认证成功，那么对userflag改成创作者，添加自媒体账号, 查询ap_user信息封装到wmuser中
     * */
    private ResponseResult createWmUserAndAuthor(AuthDto dto) {
//        根据传过来的 ApUserRealname中的id查询出ApUserRealname对象
        ApUserRealname aur = (ApUserRealname) getById(dto.getId().toString());
//        从ApUserRealname中获取user 的id 查询apuser
        ApUser apUser = apUserMapper.selectById(aur.getUserId().toString());
//       如果不存在 。。。
        if (apUser == null) {//这个判断没必要
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
//        检测自媒体用户是否存在，不存在的话创建他
        WmUser wmUser = wemediaFeign.findByName(apUser.getName());
        if (wmUser == null || wmUser.getId() == null) {
            wmUser = new WmUser();
            //设置ApUserId
            wmUser.setApUserId(apUser.getId());
            wmUser.setCreatedTime(new Date());
            wmUser.setSalt(apUser.getSalt());
            wmUser.setName(apUser.getName());
            wmUser.setPassword(apUser.getPassword());
            wmUser.setStatus(9);
            wmUser.setPhone(apUser.getPhone());
//    向wmuser中添加该对象

            wemediaFeign.save(wmUser);
        }

//如果自媒体用户表中有该用户，那么创建自媒体账号
        //创建作者账号

        createAuthor(wmUser);
        //修改ap_user标记
        apUser.setFlag((short) 1);
        apUserMapper.updateById(apUser);


//
        return null;
    }


    /*
     * 创建账号的方法
     * */
    private void createAuthor(WmUser wmUser) {
//        从wmuser中获取新增wmuser中apuser的id
        Integer apUserId = wmUser.getApUserId();
//        根据apuserid查询所对应的作者
        ApAuthor apAuthor = articleFeign.findByUserId(apUserId);
//如果找不到，新建并插入

        if (apAuthor == null) {
            apAuthor = new ApAuthor();
            apAuthor.setName(wmUser.getName());
            apAuthor.setType(2);
            apAuthor.setCreatedTime(new Date());
            apAuthor.setUserId(apUserId);
            apAuthor.setWmUserId(wmUser.getId());
            articleFeign.save(apAuthor);
        }

    }

}